package com.source.lightfowl.basedata.dao;

import com.source.lightfowl.basedata.entity.SlaughterHouse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: cxy
 * @Description: 屠宰市场持久层接口
 */
public interface SlaughterHouseDao extends JpaRepository<SlaughterHouse, Integer>, JpaSpecificationExecutor<SlaughterHouse> {

    /*@Modifying
    @Transactional
    @Query("delete from Students s where s.stuId in (?1)")
    void deleteBatch(List<Integer> ids);*/
    @Query(nativeQuery = true,
            value = "select id from slaughter_house where name = ?1"
    )
    Integer findIdByName(String name);

    @Modifying
    @Transactional
    @Query(nativeQuery = true,
     value = "update slaughter_house set state = (?1) where id in (?2)")
    void changeBatch(Integer state, ArrayList<Integer> ids);


    @Query("from SlaughterHouse s where s.state = (?1) and s.name LIKE ('%'||?2||'%')")
    List<SlaughterHouse> getLikeName(Integer state, String name);

    @Query("select distinct city from SlaughterHouse t where t.state = (?1) and t.province = (?2)")
    List<String> findCity(Integer state, String province);

    @Query("select distinct district from SlaughterHouse t where t.state = (?1) and t.province = (?2) and t.city = (?3)")
    List<String> findDistrict(Integer state, String province, String city);

    @Query("from SlaughterHouse t where t.state = (?1) and t.province = (?2) and t.city = (?3) and t.district = (?4)")
    List<SlaughterHouse> findSlaughterHouse(Integer state, String province, String city, String district);

    @Query(
            nativeQuery = true,
            value = "SELECT `name` FROM slaughter_house WHERE id=?1"
    )
    String findNameById(int houseId);

    @Query(
            nativeQuery = true,
            value = "SELECT `id` FROM slaughter_house WHERE userId=?1 LIMIT 1"
    )
    String findIdByUser(String Id);

    List<SlaughterHouse> findByState(int state);

    Page<SlaughterHouse> findByState(int state, Pageable pageable);

    SlaughterHouse findByIdAndStateNot(int id, int state);

    SlaughterHouse findByNameAndState(String name, int state);

    @Query(nativeQuery = true,
    value = "select * from slaughter_house where id = ?")
    SlaughterHouse findSlaughterById(String id);
}
